package com.lphj.datainterface.controller;

import com.lphj.datainterface.handle.CumOrderForMCHandle;
import com.lphj.datainterface.handle.CumOrderHandle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
public class ReadOrderController {

    @Autowired
    CumOrderHandle cumOrderHandle;

    @Autowired
    CumOrderForMCHandle cumOrderForMCHandle;

    /**
     * 观麦订单导入
     * @param excel
     * @return
     */
    @RequestMapping("readOrder")
    public String readExcelForOrder(@RequestParam("orderExcel") MultipartFile excel) {
        String info = "";
        boolean isExcel2003 = true;

        String fileName = excel.getOriginalFilename();
        //判断文件是否有效
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            info = "文件无效";
            return info;
        }
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = null;
        Workbook wb = null;
        try {
            is = excel.getInputStream();
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //订单有两个工作本，第一个是订单表，第二个是订单明细表
        Sheet sheet = wb.getSheetAt(0);
        Sheet sheet1 = wb.getSheetAt(1);
        info = cumOrderHandle.addCumOrder(sheet,sheet1);
        return info;
    }

    /**
     * 美菜订单导入
     * @param excel
     * @return
     */
    @RequestMapping("readOrder_mc")
    String readExcelForOrder_mc(@RequestParam("orderExcel") MultipartFile excel) {
        String info = "";
        boolean isExcel2003 = true;

        String fileName = excel.getOriginalFilename();
        //判断文件是否有效
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            info = "文件无效";
            return info;
        }
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = null;
        Workbook wb = null;
        try {
            is = excel.getInputStream();
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //订单有两个工作本，第一个是订单表，第二个是订单明细表
        Sheet sheet = wb.getSheetAt(0);
        info = cumOrderForMCHandle.addCumOrder(sheet);
        return info;
    }
}
